<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class VillagePubOperateLogs extends Model
{
    use SoftDeletes;

    protected $table = 'village_pub_operate_logs';

    /**
     * 定义主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 禁止批量赋值的字段
     *
     * @var array
     */
    protected $guarded = ['id','created_at','deleted_at'];

    /**
     * 软删除应该被调整为日期的属性
     * [$dates description]
     * @var [type]
     */
    protected $dates=[
        'created_at', 'updated_at','deleted_at'
    ];

    /*
     * @param array
     * @return bool
     */
    public static function log($param)
    {
        $user_id = auth() -> id();
        $data = [
            'village_pub_id' => $param['id'],
            'user_id' => $user_id,
            'operate_type' => $param['type'],
        ];

        if(in_array($param['type'],[2,3,4,6])){

            if($logs = self::where($data) -> first()){
                $logs -> delete();
            }else{
                self::create($data);
            }
        }else{
            $res = self::create($data);
        }

    }


}
